import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import java.lang.NullPointerException;
import java.io.IOException;

public class SlashScreen extends Canvas implements Runnable{
	
	private short width, height;
	private Image slashscreen1;
	private Image slashscreen2;
	private Display display;
	private Language language;
	private byte screen; 
	private byte menuselect;
	private boolean showslash;
	
	public SlashScreen(GameMIDlet midlet, Display display){
		this.display = display;
		language = new Language(midlet, display);
		screen = 1;
		showslash = true;
		setFullScreenMode(true);
		width = (short)getWidth();
		height = (short)getHeight();
		
		try {
			slashscreen1 = Image.createImage("/slashscreen1.png");
			slashscreen2 = Image.createImage("/slashscreen2.png");
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	protected void paint(Graphics g) {	
		if(screen == 1){
			g.setColor(0, 0, 0);
			g.fillRect(0, 0, (int)width, (int)height);
			g.drawImage(slashscreen1, 0, 0, 0);
		}
		else if(screen == 2){
			g.drawImage(slashscreen2, 0, 0, 0);
		}
	}	
	public void keyPressed(int keycode) {
		//Xu ly hien thi khi nhan de bo qua slashscreen
		if((keycode==53)||(keycode == -5)){
			screen++;	
		}
		if(screen == 3){					
			showslash = false;
			display.setCurrent(language);
		}
		repaint();
	}
	//Khong implementes Runnable, khong co ham run va showNotify thi chuong trinh van thoat khi nhan nut o tren
	public void run() {
		while(showslash){
			try {
				Thread.sleep(3000);
				screen++;				
				repaint();
				if(screen == 3){					
					showslash = false;
					display.setCurrent(language);
				}
			} 
			catch (InterruptedException e) {				
			}
		}		
	}
	
	protected void showNotify() {
		new Thread(this).start();
	}
}
